/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.properties.service.java;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ServiceDefWrapper;
import org.fireflow2.designer.eclipse.properties.IPropertyValidateListener;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class JavaServiceAttributeSection extends AbstractPropertySection implements IPropertyValidateListener{
	private AbstractEditPart editPart = null;
	private ServiceDefWrapper modelWrapper = null;
	private JavaServiceEditPanel editPanel = null;
	
	public void createControls(final Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		Composite composite = getWidgetFactory()
				.createFlatFormComposite(parent);
		editPanel = new JavaServiceEditPanel();
		editPanel.addServicePropertyModifyListener(this);
		editPanel.createControl(composite);
	}
	@Override
	public void refresh() {
		super.refresh();
		if (editPanel!=null){
			editPanel.refreshUI();
		}
	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart) {
			editPart = (AbstractEditPart) obj;
			modelWrapper = (ServiceDefWrapper) editPart.getModel();
			editPanel.setServiceDefWrapper(modelWrapper);
		}
	}	
	public void onValidateResult(boolean propertyIsOK, String errorMessage,String property,Object newValue,Object oldValue) {
		
		if (propertyIsOK){
			modelWrapper.firePropertyChange(property, oldValue, newValue);
		}else{
			MessageDialog.openError(this.editPanel.radioButton_Java.getShell(), "错误", errorMessage);
		}
		
	}	
}
